package com.easyjava.utils;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @ClassName PropertiesUitls
 * @Date 2024/5/29  11:35
 **/
public class PropertiesUitls {
    private static Properties properties = new Properties();
    private static Map<String,String> PROPER_MAP = new ConcurrentHashMap<>();

    static {
        InputStream inputStream = null;
        try {
            inputStream = PropertiesUitls.class.getClassLoader().getResourceAsStream("application.properties");
            properties.load(new InputStreamReader(inputStream,"gbk"));
            Iterator<Object> iterator = properties.keySet().iterator();
            while (iterator.hasNext()){
                String key = (String) iterator.next();
                PROPER_MAP.put(key,properties.getProperty(key));
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }finally {
            if(inputStream!=null){
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static String getString(String key){
        return PROPER_MAP.get(key);
    }

    public static void main(String[] args) {
        System.out.println(getString("db.url"));
    }

}
